package com.xinmao.quantitative.utils;

import com.xinmao.quantitative.constant.Constants;
import com.xinmao.quantitative.entity.StockDayEntity;
import com.xinmao.quantitative.entity.StockMinEntity;
import com.xinmao.quantitative.enums.EastMoneyAdjustEnum;
import com.xinmao.quantitative.enums.EastMoneyPeriodEnum;
import com.xinmao.quantitative.service.data.ILoadDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseBarSeries;

import java.time.*;
import java.util.List;

/**
 * @Title: CommComponent
 * @Author: XYe13
 * @Date: 5/6/2024 14:15
 * @Description: 记载数据工具类
 */
@Component
public class Components {
    @Autowired
    private ILoadDataService loadDataService;

    public BarSeries loadStockBarData(String symbol, String startDate, String endDate) {
        List<StockDayEntity> list = loadDataService.loadStockKline(symbol, startDate, endDate,
                EastMoneyAdjustEnum.HFQ.getType(), EastMoneyPeriodEnum.DAILY.getType());
        return getBarSeries(symbol, list);
    }

    public BarSeries loadFundBarData(String symbol, String startDate, String endDate) {
        List<StockDayEntity> list = loadDataService.loadFundKline(symbol, startDate, endDate,
                EastMoneyAdjustEnum.HFQ.getType(), EastMoneyPeriodEnum.DAILY.getType());
        return getBarSeries(symbol, list);
    }

    public BarSeries loadStockBarDataMin(String symbol, String startDate, String endDate,String adjust,String period) {
        List<StockDayEntity> list = loadDataService.loadStockKline(symbol, startDate, endDate, adjust,period);
        return getMinBarSeries(symbol, list,Integer.parseInt(period));
    }

    public BarSeries loadFundBarDataMin(String symbol, String startDate, String endDate,String adjust,String period) {
        List<StockDayEntity> list = loadDataService.loadFundKline(symbol, startDate, endDate, adjust, period);
        return getMinBarSeries(symbol, list,Integer.parseInt(period));
    }


    public BarSeries loadBarData1Min(String symbol) {
        List<StockMinEntity> list = loadDataService.loadMinKline(symbol);
        return getMinBarSeries(symbol, list);
    }

    private BarSeries getBarSeries(String symbol, List<StockDayEntity> list) {
        BarSeries series = new BaseBarSeries(symbol);
        list.forEach(stockDayEntity -> {
            ZonedDateTime date = ZonedDateTime.of(LocalDate.parse(stockDayEntity.getDate(), Constants.DATE_FORMAT), LocalTime.of(0, 0), ZoneId.systemDefault());
            series.addBar(Duration.ofDays(1), date, Double.parseDouble(stockDayEntity.getOpen()),
                    Double.parseDouble(stockDayEntity.getHigh()),
                    Double.parseDouble(stockDayEntity.getLow()),
                    Double.parseDouble(stockDayEntity.getClose()),
                    Double.parseDouble(stockDayEntity.getVolume()),
                    Double.parseDouble(stockDayEntity.getAmount()));
        });
        return series;
    }

    private BarSeries getMinBarSeries(String symbol, List<StockDayEntity> list,Integer type) {
        BarSeries series = new BaseBarSeries(symbol);
        list.forEach(stockDayEntity -> {
            ZonedDateTime date = ZonedDateTime.of(LocalDateTime.parse(stockDayEntity.getDate(), Constants.DATE_TIME_FORMAT), ZoneId.systemDefault());
            series.addBar(Duration.ofMinutes(type), date, Double.parseDouble(stockDayEntity.getOpen()),
                    Double.parseDouble(stockDayEntity.getHigh()),
                    Double.parseDouble(stockDayEntity.getLow()),
                    Double.parseDouble(stockDayEntity.getClose()),
                    Double.parseDouble(stockDayEntity.getVolume()),
                    Double.parseDouble(stockDayEntity.getAmount()));
        });
        return series;
    }

    private BarSeries getMinBarSeries(String symbol, List<StockMinEntity> list) {
        BarSeries series = new BaseBarSeries(symbol);
        list.forEach(stockMinEntity -> {
            ZonedDateTime date = ZonedDateTime.of(LocalDateTime.parse(stockMinEntity.getTime(), Constants.DATE_TIME_FORMAT), ZoneId.systemDefault());
            series.addBar(Duration.ofMinutes(EastMoneyPeriodEnum.MIN_1.getValue()), date, Double.parseDouble(stockMinEntity.getOpen()),
                    Double.parseDouble(stockMinEntity.getHigh()),
                    Double.parseDouble(stockMinEntity.getLow()),
                    Double.parseDouble(stockMinEntity.getClose()),
                    Double.parseDouble(stockMinEntity.getVolume()),
                    Double.parseDouble(stockMinEntity.getAmount()));
        });
        return series;
    }

}
